package com.live.common;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;

import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

public class LiveImageHelper extends AsyncTask<String, Integer, Bitmap> {

	private ImageView imageView;

	public LiveImageHelper(ImageView imageView) {
		this.imageView = imageView;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		Log.i("PageTask", "onPreExecute()  " + Thread.currentThread().getId());
	}

	@Override
	protected Bitmap doInBackground(String... url) {
		Log.i("PageTask", "doInBackground... " + Thread.currentThread().getId());

		if (url.length == 1) {
			return buildBitMap(url[0], null);
		}
		return buildBitMap(url[0], url[1]);
	}

	@Override
	protected void onProgressUpdate(Integer... image) {
		super.onProgressUpdate(image);
		Log.i("PageTask", "onProgressUpdate()  " + Thread.currentThread().getId());
	}

	@Override
	public void onPostExecute(Bitmap bitmap) {
		Log.i("PageTask", "onPostExecute()  " + Thread.currentThread().getId());

		this.imageView.setImageBitmap(bitmap);
	}

	public static Bitmap buildBitMap(String url, String fileName) {
		Bitmap bitmap = null;
		try {
			if (fileName == null) {
				bitmap = BitmapFactory.decodeStream(new URL(url).openStream());
			} else {
				bitmap = BitmapFactory.decodeStream(new URL(url + URLEncoder.encode(fileName, "utf-8") + ".jpg")
						.openStream());
				saveImage(bitmap, fileName);
			}
		} catch (IOException e) {
			Log.e("Exception", e.getMessage(), e);
		}
		return bitmap;
	}

	public static Bitmap buileBitMap(Context context, Uri uri, String fileName) {
		ContentResolver cr = context.getContentResolver();
		Bitmap bitmap = null;
		try {
			bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));

			saveImage(bitmap, fileName);
		} catch (FileNotFoundException e) {
			Log.e("Exception", e.getMessage(), e);
		} catch (IOException e) {
			Log.e("Exception", e.getMessage(), e);
		}
		return bitmap;
	}

	public static void saveImage(Bitmap bm, String fileName) throws IOException {
		File dirFile = new File(Constant.ALBUM_PATH);
		if (!dirFile.exists()) {
			dirFile.mkdirs();
		}
		File myCaptureFile = new File(Constant.ALBUM_PATH + fileName + ".jpg");
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
		bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
		bos.flush();
		bos.close();
	}

	public static boolean loadBitMap(ImageView iv, String fileName) {
		File file = new File(Constant.ALBUM_PATH + fileName + ".jpg");
		if (file.exists()) {
			Bitmap bit = BitmapFactory.decodeFile(Constant.ALBUM_PATH + fileName + ".jpg");
			iv.setImageBitmap(bit);
			return true;
		}
		return false;
	}
}